<template>
  <div class="app-container">
    <el-form ref="form" :model="form" label-width="120px">
      <el-form-item label="名称" prop="title" required>
        <el-input v-model="form.title" placeholder="请输入名称"/>
      </el-form-item>
      <el-form-item label="图片" prop="imgUrl" required>
        <image-upload
          :file-size="1"
          :is-multiple="false"
          :limit="1"
          :value="form.imgUrl"
          :file-type="['png', 'jpg', 'jpeg','gif']"
          @input="afterUpload"
        ></image-upload>
        <el-input v-model="form.imgUrl" placeholder="图片"/>
        <span class="el-form-item__info">图片尺寸：600*200</span>
      </el-form-item>

      <el-form-item label="排序">
        <el-input v-model="form.sort" type="number"/>
      </el-form-item>

      <el-form-item label="开始时间" prop="startTime" required>
        <el-date-picker
          v-model="form.startTime"
          type="datetime"
          placeholder="请选择"
          format="yyyy-MM-dd HH:mm:ss"
          style="width: 200px;"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="结束时间" prop="endTime" required>
        <el-date-picker
          v-model="form.endTime"
          type="datetime"
          placeholder="请选择"
          format="yyyy-MM-dd HH:mm:ss"
          style="width: 200px;"
        ></el-date-picker>
      </el-form-item>

      <el-form-item label="跳转方式">
        <el-radio-group v-model="form.linkType">
          <el-radio :label="1">内页</el-radio>
          <el-radio :label="2">tab底部导航</el-radio>
          <el-radio :label="3">外链</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item  label="跳转地址" >
        <el-input v-model="form.linkUrl" placeholder="请输入跳转地址"/>
      </el-form-item>

      <el-form-item label="状态">
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="-1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>



      <el-form-item style="margin-top: 30px">
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="goBack">返回</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import api from '@/api/banner'
import ImageUpload from '@/components/UploadFile/image.vue'

export default {
  components: {
    ImageUpload
  },
  data() {
    return {
      form: {
        title: '',
        imgUrl: '',
        linkUrl: '',
        linkType: 1, // 跳转方式，1=内页，2=tab底部导航，3=外链
        startTime: '',
        endTime: '',
        status: 1,
        id: 0

      }
    }
  },

  mounted() {
    if (this.$route.query.hasOwnProperty('id')) {
      this.getData(this.$route.query.id)
    }
  },

  methods: {
    afterUpload(list) {
      if (!list) return
      let file = list[0]
      if (file) {
        this.form.imgUrl = file.url
      }
    },
    getData(id) {
      api.info({ id: id }).then((res) => {
        this.form = res.data.info
      })
    },

    onSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          let req
          if (this.form.id > 0) {
            req = api.update(this.form)
          } else {
            req = api.create(this.form)
          }
          req.then((res) => {
            this.$message({
              type: 'success',
              message: res.msg,
              duration: 500,
              onClose: () => {
                this.$router.go(-1)
              }
            })


          }).catch((error) => {

          })
        }
      })

    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.editor-content {
  margin-top: 20px;
}

.article-textarea ::v-deep {

textarea {
  padding-right: 40px;
  resize: none;
  border: none;
  border-radius: 0px;
  border-bottom: 1px solid #bfcbd9;
}

}
</style>

